package com.qiqu;

import com.qiqu.commen.Const;
import org.springframework.boot.web.servlet.MultipartConfigFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.unit.DataSize;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.servlet.MultipartConfigElement;



@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/**")
                .addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/rbook/**").addResourceLocations("classpath:/rbook/")
                .addResourceLocations("file:///home/qiqu/rbook/");
        registry.addResourceHandler("/rbook/**").addResourceLocations("classpath:/rbook/")
        .addResourceLocations("file:///" + System.getProperties().getProperty("user.home") + "/rbook/");
        WebMvcConfigurer.super.addResourceHandlers(registry);

    }

    /**
     * 文件上传配置
     *
     * @return
     */
    @Bean
    public MultipartConfigElement multipartConfigElement() {
        MultipartConfigFactory factory = new MultipartConfigFactory();
        // 单个文件最大 KB,MB
        factory.setMaxFileSize(DataSize.parse("20480KB"));
//        /// 设置总上传数据总大小
//        factory.setMaxRequestSize(DataSize.parse("102400KB"));
        return factory.createMultipartConfig();
    }

     @Bean
     public MiniInterceptor miniInterceptor(){

        return new MiniInterceptor();
     }


    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(miniInterceptor()).addPathPatterns("/user/**").excludePathPatterns("/basic/**");

        WebMvcConfigurer.super.addInterceptors(registry);
    }

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**") //目录
                .allowedOriginPatterns("*")   //网站,全部
                .allowedMethods("*")   //哪些方法，全部
                .allowCredentials(true);//访问是否验证
    }
}
